package com.sina.microblog.ut.cases
{
	import com.sina.microblog.MicroBlog;
	import com.sina.microblog.events.MicroBlogErrorEvent;
	import com.sina.microblog.events.MicroBlogEvent;
	import com.sina.microblog.ut.utils.AssertResult;
	import com.sina.microblog.ut.utils.ResultDataUtils;
	import com.sina.microblog.ut.utils.URLRequestChecker;
	import com.sina.microblog.ut.utils.URLRequestParameters;
	
	import flash.net.URLRequest;
	
	import org.flexunit.async.Async;
	
	public class BasicAuthTest
	{
		private static const PUBLIC_TIMELINE_URL:String = "http://api.t.sina.com.cn/statuses/public_timeline.xml";
		private static const FRIENDS_TIMELINE_URL_BASE:String = "http://api.t.sina.com.cn/statuses/friends_timeline.xml";
		private static const COUNTS_URL_BASE:String = "http://api.t.sina.com.cn/statuses/counts.xml";
		
		private static const TIME_OUT:uint = 500;	
		private static var mb:MicroBlog;
		
		public function BasicAuthTest()
		{
		}
		[BeforeClass]
		public static function setUp():void
		{
			if ( null == mb )
			{
				mb = new MicroBlog();
				mb.login("tester@test.com", "tester");
			}
		}
		
		// Public timeline
		[Test(async,timeout="20000")]
		public function testPublicTimeline():void
		{
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_PUBLIC_TIMELINE_RESULT, AssertResult.assertStatusArray, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters( mb.source);
			var url:String = PUBLIC_TIMELINE_URL + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadPublicTimeline();
			URLRequestChecker.check(mb, req, ResultDataUtils.statusArray);
		}
		[Test(async,timeout="20000")]
		public function testPublicTimelineFail():void
		{
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_PUBLIC_TIMELINE_ERROR, AssertResult.assertError, TIME_OUT);
			var params:URLRequestParameters = new URLRequestParameters( mb.source);
			var url:String = PUBLIC_TIMELINE_URL + params.toGetString();
			var req:URLRequest = new URLRequest(url);
			mb.loadPublicTimeline();
			URLRequestChecker.check(mb, req, ResultDataUtils.error);
			
			
		}
		
		//Friends timeline
		[Test(async,timeout="20000")]
		public function testFriendsTimelineWithoutParams():void
		{
			var params:URLRequestParameters = new URLRequestParameters( mb.source);
			var url:String = FRIENDS_TIMELINE_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_FRIENDS_TIMELINE_RESULT, AssertResult.assertStatusArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsTimeline();
			URLRequestChecker.check(mb, req, ResultDataUtils.statusArray);			
			
		}
		[Test(async,timeout="20000")]
		public function testFriendsTimelineFail():void
		{
			var params:URLRequestParameters = new URLRequestParameters( mb.source);
			var url:String = FRIENDS_TIMELINE_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_FRIENDS_TIMELINE_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadFriendsTimeline();
			URLRequestChecker.check(mb, req, ResultDataUtils.error);			
			
		}
		
		//Test Counts.
		[Test(async,timeout="20000")]
		public function testCounts():void
		{
			var ids:Array = [1,233,4555,64545];
			var idStr:String = ids.join(",");
			var params:URLRequestParameters = new URLRequestParameters( mb.source);
			params.addParam("ids", idStr);
			var url:String =COUNTS_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogEvent.LOAD_STATUS_COUNTS_RESULT, AssertResult.assertCountArray, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadStatusCounts(ids);
			URLRequestChecker.check(mb, req, ResultDataUtils.counts);			
			
		}
		[Test(async,timeout="20000")]
		public function testCountsFail():void
		{
			var ids:Array = [1,233,4555,64545];
			var idStr:String = ids.join(",");
			var params:URLRequestParameters = new URLRequestParameters(mb.source);
			params.addParam("ids", idStr);
			var url:String =COUNTS_URL_BASE + params.toGetString();
			Async.handleEvent(this, mb, MicroBlogErrorEvent.LOAD_STATUS_COUNTS_ERROR, AssertResult.assertError, TIME_OUT);
			
			var req:URLRequest = new URLRequest(url);
			mb.loadStatusCounts(ids);
			URLRequestChecker.check(mb, req, ResultDataUtils.error);			
			
		}
	}
}